const app = getApp();

Page({
  data: {
    activeTab: 'recommend',
    dynamicList: [],
    page: 1,
    hasMore: true,
    loading: false,
    isProfileComplete: false,
    cdnUrl:''
  },

  onLoad() {
    this.setData({
      isProfileComplete: app.checkProfileComplete(),
      cdnUrl:app.globalData.cdnUrl
    });
    this.loadDynamicList();
  },

  onShow() {
    this.setData({
      isProfileComplete: app.checkProfileComplete()
    });
  },

  // 切换Tab
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({
      activeTab: tab,
      page: 1,
      hasMore: true,
      dynamicList: []
    });
    this.loadDynamicList();
  },

  // 加载动态列表
  loadDynamicList(isRefresh) {
    if (this.data.loading) return;
    this.setData({ loading: true });
    var _this = this
    wx.request({
        url: app.globalData.baseUrl + 'public/trend-list',
        data: {
          page: _this.data.page,
        },
        success: function(res) {
          if (res.data.code === 0) {
            const list = res.data.data.needs || []
            const newList = isRefresh ? list : _this.data.dynamicList.concat(list)
            
            _this.setData({
              dynamicList: newList,
              hasMore: list.length === 5,
              loading: false
            })
          }
        },
        complete: function() {
            _this.setData({
            loading: false,
          })
        }
    })
    
      return
    // 模拟API调用
    setTimeout(() => {
      //const mockData = this.getMockDynamicData();
      
      this.setData({
        dynamicList: this.data.page === 1 ? mockData : [...this.data.dynamicList, ...mockData],
        loading: false,
        hasMore: this.data.page < 3
      });
    }, 1000);
  },

  // 获取模拟动态数据
  getMockDynamicData() {
    const mockDynamics = [
      {
        id: 1,
        userId: 1,
        nickname: '温柔如风',
        age: 58,
        city: '北京市朝阳区',
        avatar: '/images/avatar/11.jpg',
        datingType: 'partner',
        content: '今天去公园散步，看到很多老人在跳舞，感觉生活很美好。希望能找到一个志同道合的伴侣一起享受晚年生活。',
        images: ['/images/avatar/11.jpg'],
        createTime: '2小时前',
        likeCount: 12,
        commentCount: 3,
        isLiked: false
      },
      {
        id: 2,
        userId: 2,
        nickname: '阳光大叔',
        age: 62,
        city: '北京市海淀区',
        avatar: '/images/avatar/22.jpg',
        datingType: 'partner',
        content: '退休后开始学习太极拳，感觉身体越来越好了。有同样爱好的朋友可以一起交流。',
        images: ['/images/avatar/22.jpg', '/images/avatar/22.jpg'],
        createTime: '4小时前',
        likeCount: 8,
        commentCount: 2,
        isLiked: true
      },
      {
        id: 3,
        userId: 3,
        nickname: '优雅女士',
        age: 55,
        city: '北京市西城区',
        avatar: '/images/avatar/33.jpg',
        datingType: 'friend',
        content: '今天做了红烧肉，味道不错。喜欢烹饪的朋友可以一起分享美食心得。',
        images: ['/images/avatar/33.jpg'],
        createTime: '6小时前',
        likeCount: 15,
        commentCount: 5,
        isLiked: false
      }
    ];
    
    return mockDynamics;
  },

  // 查看动态详情
  viewDynamic(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/dynamic-detail/dynamic-detail?id=${id}`
    });
  },

  // 查看用户资料
  viewUserProfile(e) {
    const userId = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: `/pages/user-detail/user-detail?id=${userId}`
    });
  },

  // 预览图片
  previewImage(e) {
    const urls = e.currentTarget.dataset.urls;
    const current = e.currentTarget.dataset.current;
    wx.previewImage({
      urls: urls,
      current: current
    });
  },

  // 点赞/取消点赞
  toggleLike(e) {
    const id = e.currentTarget.dataset.id;
    const dynamicList = this.data.dynamicList;
    const index = dynamicList.findIndex(item => item.id === id);
    
    if (index > -1) {
      const item = dynamicList[index];
      item.isLiked = !item.isLiked;
      item.likeCount = item.isLiked ? item.likeCount + 1 : item.likeCount - 1;
      
      this.setData({
        dynamicList: dynamicList
      });
    }
  },

  // 查看评论
  viewComments(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/dynamic-detail/dynamic-detail?id=${id}&tab=comments`
    });
  },

  // 加载更多
  loadMore() {
    if (!this.data.hasMore || this.data.loading) return;
    
    this.setData({
      page: this.data.page + 1
    });
    
    this.loadDynamicList();
  },

  // 去完善资料
  goToEditProfile() {
    wx.navigateTo({
      url: '/pages/edit-profile/edit-profile'
    });
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.setData({
      page: 1,
      hasMore: true
    });
    this.loadDynamicList();
    wx.stopPullDownRefresh();
  },

  // 上拉加载
  onReachBottom() {
    if (this.data.hasMore && !this.data.loading) {
      this.loadMore();
    }
  },

  // 跳转发布动态
  goToPublish() {
    console.log('goToPublish tapped');
    wx.navigateTo({
      url: '/pages/publish-trend/publish-trend'
    });
  },

  // 兼容绑定别名
  handlePublishTap() {
    this.goToPublish();
  }
}); 